package MyServlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.HospitalDao;
import MyBean.ChuFangDrug;
import MyBean.Message;
import factory.HospitalFactory;

/**
 * Servlet implementation class insertchufangdrug
 */

public class insertChuFangDrug extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public insertChuFangDrug() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		try {
			HospitalDao hd = HospitalFactory.getProxy();
			String drugName = request.getParameter("drugName");
			Message message = hd.searchMessageByName(drugName);
				if(message!=null&&hd.insertChuFangDrug(new ChuFangDrug(request.getParameter("admissionNumber"),request.getParameter("drugName"),message.getPrice(),request.getParameter("dosage"))))
				{
					
					RequestDispatcher dispatcher = request.getRequestDispatcher("insertChuFangDrugSuccess.jsp");
					dispatcher.forward(request, response);
				} else {
					 response.sendRedirect("insertChuFangDrugFail.jsp");
				}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
